package com.yic.module.device.controller.admin.inspectionDeviceEntry;

import com.yic.module.device.dal.dataobject.inspectionPlanEntry.InspectionPlanEntryDO;
import com.yic.module.device.service.inspectionPlanEntry.InspectionPlanEntryService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.inspectionDeviceEntry.vo.*;
import com.yic.module.device.dal.dataobject.inspectionDeviceEntry.InspectionDeviceEntryDO;
import com.yic.module.device.convert.inspectionDeviceEntry.InspectionDeviceEntryConvert;
import com.yic.module.device.service.inspectionDeviceEntry.InspectionDeviceEntryService;

@Tag(name = "管理后台 - 设备与检查项关联")
@RestController
@RequestMapping("/device/inspection-device-entry")
@Validated
public class InspectionDeviceEntryController {

    @Resource
    private InspectionDeviceEntryService inspectionDeviceEntryService;

    @Resource
    private InspectionPlanEntryService inspectionPlanEntryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备与检查项关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:create')")
    public CommonResult<Long> createInspectionDeviceEntry(@Valid @RequestBody InspectionDeviceEntryCreateReqVO createReqVO) {
        return success(inspectionDeviceEntryService.createInspectionDeviceEntry(createReqVO));
    }

    @PostMapping("/createBatch")
    @Operation(summary = "批量创建设备与检查项关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:create')")
    public CommonResult<Void> createBatchInspectionDeviceEntry(@Valid @RequestBody List<InspectionDeviceEntryCreateReqVO> createReqVO) {
        return success(inspectionDeviceEntryService.createBatchInspectionDeviceEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备与检查项关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:update')")
    public CommonResult<Boolean> updateInspectionDeviceEntry(@Valid @RequestBody InspectionDeviceEntryUpdateReqVO updateReqVO) {
        inspectionDeviceEntryService.updateInspectionDeviceEntry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备与检查项关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:delete')")
    public CommonResult<Boolean> deleteInspectionDeviceEntry(@RequestParam("id") Long id) {
        inspectionDeviceEntryService.deleteInspectionDeviceEntry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备与检查项关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:query')")
    public CommonResult<InspectionDeviceEntryRespVO> getInspectionDeviceEntry(@RequestParam("id") Long id) {
        InspectionDeviceEntryDO inspectionDeviceEntry = inspectionDeviceEntryService.getInspectionDeviceEntry(id);
        return success(InspectionDeviceEntryConvert.INSTANCE.convert(inspectionDeviceEntry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备与检查项关联列表")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:query')")
    public CommonResult<List<InspectionDeviceEntryRespVO>> getInspectionDeviceEntryList(@Valid InspectionDeviceEntryExportReqVO exportReqVO) {
        List<InspectionDeviceEntryDO> list = inspectionDeviceEntryService.getInspectionDeviceEntryList(exportReqVO);
        return success(InspectionDeviceEntryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备与检查项关联分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:query')")
    public CommonResult<PageResult<InspectionDeviceEntryRespVO>> getInspectionDeviceEntryPage(@Valid InspectionDeviceEntryPageReqVO pageVO) {
        PageResult<InspectionDeviceEntryDO> pageResult = inspectionDeviceEntryService.getInspectionDeviceEntryPage(pageVO);
        PageResult<InspectionDeviceEntryRespVO> result = InspectionDeviceEntryConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(item->{
            InspectionPlanEntryDO entry = inspectionPlanEntryService.getInspectionPlanEntry(item.getEntryId());
            item.setName(entry.getName());
            item.setCode(entry.getCode());
            item.setStatus(entry.getStatus());
            item.setContent(entry.getContent());
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备与检查项关联 Excel")
    @PreAuthorize("@ss.hasPermission('device:inspection-device-entry:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectionDeviceEntryExcel(@Valid InspectionDeviceEntryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectionDeviceEntryDO> list = inspectionDeviceEntryService.getInspectionDeviceEntryList(exportReqVO);
        // 导出 Excel
        List<InspectionDeviceEntryExcelVO> datas = InspectionDeviceEntryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备与检查项关联.xls", "数据", InspectionDeviceEntryExcelVO.class, datas);
    }

}
